package model.dba;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;

import model.Alumno;

public class Statem<Object> implements Callable<List<Object>> {

	private String query;
	private PreparedStatement preparedStatement;
	private Connection conection;
	private Class<Object> objectClass;
	private Properties config;

	public Statem() {

		this.query = null;

	}

	private List<Object> execute() {

		List<Object> objectList = new ArrayList<Object>();
		try {
			preparedStatement = conection.prepareStatement(this.query);
			boolean traeResulset = preparedStatement.execute();
			if (traeResulset) {
				ResultSet resulset = preparedStatement.getResultSet();
				objectList = obtainObjects(resulset);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return objectList;
	}

	private List<Object> obtainObjects(ResultSet resultSet) {

		List<Object> objectList = new ArrayList<Object>();

		try {
			while (resultSet.next()) {
				Object object = null;
				try {
					object = (Object) objectClass.newInstance();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}

				if (object instanceof Alumno) {

					/*
					 * object.setNombre(resultSet.getString("nombre"));
					 * object.setApellido(resultSet.getString("apellido"));
					 * object.setId((int) resultSet.getLong("id"));
					 * object.setTipo(resultSet.getString("tipo"));
					 * object.setDni(resultSet.getString("dni"));
					 * object.setClave(resultSet.getString("clave"));
					 * object.setDni(resultSet.getString("dni"));
					 * object.setMatricula(resultSet.getString("matricula"));
					 * object
					 * .setEspecialidad(resultSet.getString("especialidad"));
					 * object.setObras_sociales(Get.obrasSociales(nuevo));
					 */
				}
				objectList.add(object);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return objectList;

	}

	@Override
	public List<Object> call() throws Exception {

		return execute();
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public void setNecesary(String query, Connection conection, Class<Object> Class) {
		this.query = query;
		this.conection = conection;
		this.objectClass = Class;
	}

}
